package zzu.websocket.JAXB;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author:ny
 * @Date: 2020/4/13 10:13
 */
public class UnitTest {

    public static void main(String[] args) throws JAXBException, IOException {
        test();
//        test2();
    }

    public static void test() throws JAXBException, IOException {
        //获得转换的上下文对象
        JAXBContext context = JAXBContext.newInstance(AddressList.class);
        //获得Marshaller对象
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        AddressList addressList = new AddressList();
//        init(addressList);
        loadData(addressList);
        System.out.println(addressList);
        FileWriter writer = new FileWriter(new File("address.xml"));
        System.out.println(addressList);
        marshaller.marshal(addressList, writer);
        marshaller.marshal(addressList, System.out);
    }

    private static void loadData(AddressList addressList) {
        ArrayList<Unit> units1 = new ArrayList<>();
        Unit unit = new Unit();
        unit.setID("b000425747479d528d6db9181de9");
        unit.setName("模拟指挥所");
        CommElement commElement = new CommElement();
        commElement.BYNAME = unit.getName();
        commElement.JDLX = "0";
        unit.setCommElement(commElement);

        Unit unit1 = new Unit();
        unit1.setID("390066eaa9be40d5910486157da1fb28");
        unit1.setName("要素室一");
        CommElement commElement1 = new CommElement();
        commElement1.BYNAME = unit.getName();
        commElement1.JDLX = "0";
        unit1.setCommElement(commElement1);
        ArrayList<Unit> units = new ArrayList<>();
        units.add(unit1);
        unit.setList(units);


        Unit unit2 = new Unit();
        unit2.setID("b000425747479d528d6db9181de9");
        unit2.setName("123");
        CommElement commElement2 = new CommElement();
        commElement2.BYNAME = unit2.getName();
        commElement2.JDLX = "0";
        unit2.setCommElement(commElement2);

        units1.add(unit);
        units1.add(unit2);
        addressList.setUnit(units1);
    }

    private static void init(AddressList addressList) {

        addressList.setTimeStamp("20141103154133944");
        addressList.setVersion("1.0.0.1");
        CommElementType commElementType = new CommElementType("VOIPDH", "VOIP电话", "");
        CommElementType commElementType2 = new CommElementType("CMDPOSTID", "指挥所标识", "本节点隶属的指挥所标识");
        CommElementType commElementType3 = new CommElementType("SeatID", "席位编码", "席位类别中的席位编码");
        CommElementType commElementType4 = new CommElementType("SeatName", "席位名称", "席位名称");
        CommElementType commElementType5 = new CommElementType("Telephone", "电话号码", "名录项的电话号码");
        CommElementType commElementType6 = new CommElementType("IP", "IP地址", "");
        CommElementType commElementType7 = new CommElementType("SecretMc", "保密机号", "保密模块的唯一标识");
        CommElementType commElementType8 = new CommElementType("JDLX", "节点类型", "0=指挥所，1=组节点，2=部门，3=职务 （角色），4=席位，5=用户，6=客户机，7=服务器，8=人员，9=其他，10=武器平台");
        CommElementType commElementType9 = new CommElementType("BMJ", "保密机标识号", "用户保密机标识号");
        CommElementType commElementType10 = new CommElementType("BYNAME", "别名", "本节点的显示别名");
        CommElementType commElementType11 = new CommElementType("J链平台编识号", "JPTBSH", "");
        CommElementType commElementType12 = new CommElementType("JIDSPTLX", "JIDS数据链平台类型", "");
        CommElementType commElementType13 = new CommElementType("JIDSPTStatus", "JIDS数据链平台状态", "");
        CommElementType commElementType14 = new CommElementType("MHS", "文电地址", "");
        CommElementType commElementType15 = new CommElementType("DM", "短报地址", "");

        ArrayList<CommElementType> list = new ArrayList<>();
        list.add(commElementType);
        list.add(commElementType2);
        list.add(commElementType3);
        list.add(commElementType4);
        list.add(commElementType5);
        list.add(commElementType6);
        list.add(commElementType7);
        list.add(commElementType8);
        list.add(commElementType9);
        list.add(commElementType10);
        list.add(commElementType11);
        list.add(commElementType12);
        list.add(commElementType13);
        list.add(commElementType14);
        list.add(commElementType15);
        addressList.setCommElementTypeList(list);
    }

    public static void test2() throws IOException, JAXBException {
        //获得转换的上下文对象
        JAXBContext context = JAXBContext.newInstance(Unit.class);
        //获得Marshaller对象
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        Unit unit = new Unit();
        unit.setID("b000425747479d528d6db9181de9");
        unit.setName("模拟指挥所");
        CommElement commElement = new CommElement();
        commElement.BYNAME = unit.getName();
        commElement.JDLX = "0";
        unit.setCommElement(commElement);

        Unit unit1 = new Unit();
        unit1.setID("390066eaa9be40d5910486157da1fb28");
        unit1.setName("要素室一");
        CommElement commElement1 = new CommElement();
        commElement1.BYNAME = unit.getName();
        commElement1.JDLX = "0";
        unit1.setCommElement(commElement1);
        ArrayList<Unit> units = new ArrayList<>();
        units.add(unit1);
        unit.setList(units);
        System.out.println(unit);
        FileWriter writer = new FileWriter(new File("unit.xml"));
        marshaller.marshal(unit, writer);
        marshaller.marshal(unit, System.out);
    }
}
